const Seller = require('../model/Seller');
const Buller = require('../model/Buller');
const Good = require('../model/Good');
const Follow = require('../model/Follow');
const Like = require('../model/Likes');
const Desc = require('../model/Desc');
const Reply = require('../model/Reply');
const Message = require('../model/Message');

exports.getMsgs = (req,res,nent)=>{

    if(!req.session.buller){
        return res.end('fail');
    }
    var target_id = req.session.buller._id;

    Message.getBullerMessages(target_id,(err,messages)=>{

        if(err){
            return res.end(err);
        }

        res.json({msg:'success',messages});



    })


}

exports.getSellerMsgs = (req,res,nent)=>{

    if(!req.session.seller){
        return res.end('fail');
    }
    var target_id = req.session.seller._id;

    Message.getSellerMessages(target_id,(err,messages)=>{

        if(err){
            return res.end(err);
        }

        res.json({msg:'success',messages});



    })


}


exports.readMsg=(req,res,next)=>{
    var id = req.query.id;
    Message.getMsgById(id,(err,msg)=>{
        if(err){
            return res.end(err);
        }

        msg.hasRead = true;
        msg.save().then((msg)=>{
            var target_id = req.session.buller._id;

            Message.getBullerMessages(target_id,(err,messages)=>{

                if(err){
                    return res.end(err);
                }

                res.json({msg:'success',messages});



            })



        })




    })



}
exports.removeMsg = (req,res,next)=>{
    var id = req.query.id;
    var target_id = req.session.buller._id;
    Message.remove({'_id':id}).then((msg)=>{



        Message.getBullerMessages(target_id,(err,messages)=>{

            if(err){
                return res.end(err);
            }

            res.json({msg:'success',messages});



        })



    })

}



exports.readSellerMsg=(req,res,next)=>{
    var id = req.query.id;
    Message.getMsgById(id,(err,msg)=>{
        if(err){
            return res.end(err);
        }

        msg.hasRead = true;
        msg.save().then((msg)=>{
            var target_id = req.session.seller._id;

            Message.getSellerMessages(target_id,(err,messages)=>{

                if(err){
                    return res.end(err);
                }

                res.json({msg:'success',messages});



            })



        })




    })



}
exports.removeSellerMsg = (req,res,next)=>{
    var id = req.query.id;
    var target_id = req.session.seller._id;
    Message.remove({'_id':id}).then((msg)=>{



        Message.getSellerMessages(target_id,(err,messages)=>{

            if(err){
                return res.end(err);
            }

            res.json({msg:'success',messages});



        })



    })

}
